package com.lemon.smartsafetynew.ui.adapter.task;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.DateUtils;
import com.lemon.smartsafetynew.entity.task.InspectionRecordVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by JiangAn on 2018/5/9.
 */

public class RecordAdapter extends LemonRcvAdapter<InspectionRecordVo> {

    private RecyclerTClickListener<InspectionRecordVo> mListener;

    public RecordAdapter(@Nullable List<InspectionRecordVo> data, RecyclerTClickListener<InspectionRecordVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new RecordAdapter.Item();
    }

    public class Item implements AdapterItem<InspectionRecordVo> {
        @Bind(R.id.tv_inspection_type)
        TextView tvInspectionType;
        @Bind(R.id.tv_task_time)
        TextView tvTaskTime;
        @Bind(R.id.tv_task_people)
        TextView tvTaskPeople;
        @Bind(R.id.tv_task_result)
        TextView tvTaskResult;
        @Bind(R.id.linear_layout_task)
        LinearLayout linearLayoutTask;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.iv_left)
        ImageView ivLeft;
        private InspectionRecordVo data;
        private int pos;

        @Override
        public int getLayoutResId() {
            return R.layout.item_inspection_task;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            linearLayoutTask.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data, pos);
                }
            });

        }

        @Override
        public void handleData(InspectionRecordVo inspectionRecordVo, int position) {
            data = inspectionRecordVo;
            pos = position;
            tvTaskResult.setVisibility(View.GONE);
            tvInspectionType.setText(CommonUtil.avoidNullString(inspectionRecordVo.getObject_title()));
            tvTaskPeople.setText(CommonUtil.avoidNullString(inspectionRecordVo.getCheck_user_name()));
            if (inspectionRecordVo.getCheck_date() != null) {
                String checkDate = DateUtils.getStringByFormat(new Date(inspectionRecordVo.getCheck_date()), "yyyy-MM-dd HH:mm:ss");
                tvTaskTime.setText(checkDate);
            }
            ivLeft.setVisibility(View.VISIBLE);
            tvStatus.setVisibility(View.VISIBLE);
            if (inspectionRecordVo.getStatus() != null) {
                if (inspectionRecordVo.getStatus() == 1) {
                    tvStatus.setText("正常");
                    tvStatus.setBackgroundResource(R.drawable.formal_circle_shape);
                } else if (inspectionRecordVo.getStatus() == 2) {
                    tvStatus.setText("异常");
                    tvStatus.setBackgroundResource(R.drawable.noformal_circle_shape);
                }
            }
        }
    }
}
